import 'package:flutter/material.dart';
import 'package:flutter_one/components/child_comp.dart';

class StatefulComp extends StatefulWidget {
  const StatefulComp({super.key, required this.intro, required this.offer});

  // 定义属性接收父组件传值
  final String intro;
  final String offer;

  @override
  State<StatefulComp> createState() {
    print('父组件---createState');
    return _StatefulCompState();
  }
}

class _StatefulCompState extends State<StatefulComp> {
  int _count = 10;

  // 要获取上面的属性，则需要使用widget.属性名

  @override
  void initState() {
    super.initState();
    print('父组件---initState');
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    print('父组件---didChangeDependencies');
  }

  @override
  Widget build(BuildContext context) {
    print('父组件---build');
    return Column(
      children: [
        Text('有状态组件, 父组件传的值: ${widget.intro}, 职业: ${widget.offer}'),
        Row(
          children: [
            ElevatedButton(
                onPressed: () {
                  setState(() {
                    _count--;
                  });
                },
                child: const Text('-')),
            const SizedBox(width: 10),
            Text(_count.toString()),
            const SizedBox(width: 10),
            ElevatedButton(
                onPressed: () {
                  setState(() {
                    _count++;
                  });
                },
                child: const Text('+')),
          ],
        ),
        // 使用子组件
        ChildComp(
            count: _count,
            updateCount: (int count) {
              setState(() {
                _count = count;
              });
            }),
      ],
    );
  }

  @override
  void didUpdateWidget(covariant StatefulComp oldWidget) {
    super.didUpdateWidget(oldWidget);
    print('父组件---didUpdateWidget');
  }

  @override
  void deactivate() {
    super.deactivate();
    print('父组件---deactivate');
  }

  @override
  dispose() {
    super.dispose();
    print('父组件---dispose');
  }
}
